package ch11.d9;

import java.util.LinkedList;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MyThreadTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {

        Scanner scan=new Scanner(System.in);
        LinkedList<MyThread> list=new LinkedList();
        while (true) {
            int n=scan.nextInt();
            if(n==1){
                MyThread t=new MyThread();
                list.add(t);
               t.start();
            }else if(n==2){
               Thread t=list.pop(); // 从队列中取出一个线程
                t.stop();
            }else if(n==3){
                //暂停
                for (int i = list.size()-1; i >=0 ; i--) {
                    MyThread t=list.get(i);
                    if(t.flag==false){
                        t.flag=true;
                        break;
                    }
                }

            }else if(n==4){
                //继续
                synchronized (MyThread.o) {
                    MyThread.o.notify();
                }

            }
        }


    }
}
